package net.bsayiner.foreignExchange.Activities;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;

import net.bsayiner.foreignExchange.R;
import net.bsayiner.foreignExchange.Utilities.Currency;
import net.bsayiner.foreignExchange.Utilities.CurrencyReaderFromTcmb;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class TcmbCurrencyActivity extends Activity implements Runnable {

	private CurrencyReaderFromTcmb currencyReaderFromTcmb;
	private ProgressDialog currencyUpdateDialog;
	private ArrayList<Currency> currencies;
	private NumberFormat currencyFormat;

	private TextView txtUsdBuy;
	private TextView txtUsdSell;
	private TextView txtEurBuy;
	private TextView txtEurSell;
	private TextView txtChfBuy;
	private TextView txtChfSell;
	private TextView txtGbpBuy;
	private TextView txtGbpSell;
	private TextView txtDkkBuy;
	private TextView txtDkkSell;
	private TextView txtSekBuy;
	private TextView txtSekSell;
	private TextView txtNokBuy;
	private TextView txtNokSell;
	private TextView txtJpyBuy;
	private TextView txtJpySell;
	private TextView txtSarBuy;
	private TextView txtSarSell;
	private TextView txtAudBuy;
	private TextView txtAudSell;
	private TextView txtCadBuy;
	private TextView txtCadSell;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.currency_layout);
		initializeVariables();
		updateCurrencyData();
	}

	private void refreshCurrencyData() {
		currencyUpdateDialog = ProgressDialog.show(this, "", "Kur bilgisi güncelleniyor...");
		Thread thread = new Thread(this);
		thread.start();
	}

	private void initializeVariables() {
		currencyReaderFromTcmb = new CurrencyReaderFromTcmb();
		currencies = getIntent().getParcelableArrayListExtra("Currency");
		currencyFormat = NumberFormat.getNumberInstance(Locale.US);
		currencyFormat.setGroupingUsed(false);
		currencyFormat.setMinimumFractionDigits(2);
		currencyFormat.setMaximumFractionDigits(2);
		txtUsdBuy = (TextView) findViewById(R.id.txt_usd_buy);
		txtUsdSell = (TextView) findViewById(R.id.txt_usd_sell);
		txtEurBuy = (TextView) findViewById(R.id.txt_eur_buy);
		txtEurSell = (TextView) findViewById(R.id.txt_eur_sell);
		txtChfBuy = (TextView) findViewById(R.id.txt_chf_buy);
		txtChfSell = (TextView) findViewById(R.id.txt_chf_sell);
		txtGbpBuy = (TextView) findViewById(R.id.txt_gbp_buy);
		txtGbpSell = (TextView) findViewById(R.id.txt_gbp_sell);
		txtDkkBuy = (TextView) findViewById(R.id.txt_dkk_buy);
		txtDkkSell = (TextView) findViewById(R.id.txt_dkk_sell);
		txtSekBuy = (TextView) findViewById(R.id.txt_sek_buy);
		txtSekSell = (TextView) findViewById(R.id.txt_sek_sell);
		txtNokBuy = (TextView) findViewById(R.id.txt_nok_buy);
		txtNokSell = (TextView) findViewById(R.id.txt_nok_sell);
		txtJpyBuy = (TextView) findViewById(R.id.txt_jpy_buy);
		txtJpySell = (TextView) findViewById(R.id.txt_jpy_sell);
		txtSarBuy = (TextView) findViewById(R.id.txt_sar_buy);
		txtSarSell = (TextView) findViewById(R.id.txt_sar_sell);
		txtAudBuy = (TextView) findViewById(R.id.txt_aud_buy);
		txtAudSell = (TextView) findViewById(R.id.txt_aud_sell);
		txtCadBuy = (TextView) findViewById(R.id.txt_cad_buy);
		txtCadSell = (TextView) findViewById(R.id.txt_cad_sell);
	}

	public void run() {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		currencies = currencyReaderFromTcmb.getCurrenciesFromWeb();
		handler.sendEmptyMessage(0);
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			updateCurrencyData();
			currencyUpdateDialog.dismiss();
		}
	};

	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.menu_structure, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_update:
			refreshCurrencyData();
			return true;
		case R.id.menu_about:
			PackageInfo packageInfo = null;
			try {
				packageInfo = getPackageManager().getPackageInfo("net.bsayiner.foreignExchange", 0);
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
			final AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setTitle("Türk Lirası v" + packageInfo.versionName);
			alert.setMessage("Bu program Bora SAYINER tarafından yazılmış ve  OSI onaylı açık kaynak lisansı altında dağıtılmaktadır. Soru ve görüşleriniz için bsayiner@bsayiner.net adresine e-posta gönderebilirsiniz.");
			alert.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			alert.show();
			return true;
		case R.id.menu_exit:
			System.exit(0);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void updateCurrencyData() {
		txtEurBuy.setText(currencyFormat.format(currencies.get(1).getBuying()));
		txtEurSell.setText(currencyFormat.format(currencies.get(1).getSelling()));
		txtUsdBuy.setText(currencyFormat.format((currencies.get(2).getBuying())));
		txtUsdSell.setText(currencyFormat.format(currencies.get(2).getSelling()));
		txtCadBuy.setText(currencyFormat.format(currencies.get(3).getBuying()));
		txtCadSell.setText(currencyFormat.format(currencies.get(3).getSelling()));
		txtAudBuy.setText(currencyFormat.format(currencies.get(4).getBuying()));
		txtAudSell.setText(currencyFormat.format(currencies.get(4).getSelling()));
		txtDkkBuy.setText(currencyFormat.format(currencies.get(5).getBuying()));
		txtDkkSell.setText(currencyFormat.format(currencies.get(5).getSelling()));
		txtSekBuy.setText(currencyFormat.format(currencies.get(6).getBuying()));
		txtSekSell.setText(currencyFormat.format(currencies.get(6).getSelling()));
		txtNokBuy.setText(currencyFormat.format(currencies.get(7).getBuying()));
		txtNokSell.setText(currencyFormat.format(currencies.get(7).getSelling()));
		txtJpyBuy.setText(currencyFormat.format(currencies.get(8).getBuying()));
		txtJpySell.setText(currencyFormat.format(currencies.get(8).getSelling()));
		txtChfBuy.setText(currencyFormat.format(currencies.get(9).getBuying()));
		txtChfSell.setText(currencyFormat.format(currencies.get(9).getSelling()));
		txtSarBuy.setText(currencyFormat.format(currencies.get(10).getBuying()));
		txtSarSell.setText(currencyFormat.format(currencies.get(10).getSelling()));
		txtGbpBuy.setText(currencyFormat.format(currencies.get(11).getBuying()));
		txtGbpSell.setText(currencyFormat.format(currencies.get(11).getSelling()));
	}
}
